package cn.onlineoedering.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;

import cn.onlineoedering.user.entity.GeneralUser;

public class CommonXmlUtil {
	
	public static void saveUserToXml(String path,GeneralUser user) {
		System.out.println("saveUserToXml");
		File file=new File(path);
		try {
			//excel输出格式化
			OutputFormat format =OutputFormat.createCompactFormat();
			XMLWriter writer=new XMLWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"),format);
			writer.write(creatXml(user,"E:\\user.xml"));
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@SuppressWarnings("deprecation")
	@Test
	public static Document creatXml(GeneralUser generalUser ,String path){
		//获取文档对象
		Document document=getDocument(path);
		System.out.println(document);
		System.out.println("==============");
		//添加xml文件的根元素
		Element usersElement=document.addElement("用户");
		SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
		usersElement.addComment("真是保存用户信息的xml文件，创建于："+dateFormat.format(new Date()));
		//在根元素下添加子元素
		Element userNameElement=usersElement.addElement("用户名");
		//为“用户 名”元素添加属性
		userNameElement.setAttributeValue("name", generalUser.getUserName());
		Element passwordeElement=userNameElement.addElement("密码");
		//
		passwordeElement.setText(generalUser.getPassword());
		Element roleElement=userNameElement.addElement("角色");
		//
		roleElement.setText(generalUser.getRole());
		
		Element genderElement=userNameElement.addElement("性别");
		//
		genderElement.setText(generalUser.getGender());
		Element phoneElement=userNameElement.addElement("联系电话");
		//�O��phoneElementԪ�صă���
		phoneElement.setText(generalUser.getPhone());
		Element qqElement=userNameElement.addElement("QQ");
		//�O��qqElementԪ�صă���
		qqElement.setText(generalUser.getQQ());
		Element cardIdElement=userNameElement.addElement("身份证");
		//�O��cardIdElementԪ�صă���
		cardIdElement.setText(generalUser.getCardId());
		Element addressElement=userNameElement.addElement("地址");
		//�O��addressElementԪ�صă���
		addressElement.setText(generalUser.getAddress());
		return document;
	}
	
	/**
	 * 读取xml文件
	 */
	public static Document getDocument(String path) {
		Document document=null;
		try {
			if (path=="" || path.equals("") ||path==null) {
				document=DocumentHelper.createDocument();
			}else{
				FileInputStream inputStream = new FileInputStream(new File(
						path));
				SAXReader reader = new SAXReader();
				document=reader.read(inputStream);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return document;
	}
	
	public static void readXml() {
//		Document document=getDocument("E:\\user.xml");
	}
	public static void main(String[] args) {
		String path="E:\\user.xml";
		GeneralUser generalUsers=new GeneralUser(1,"zhangsan","1234","管理员","男","18501369543","403434305","450328198905012712","北京海淀");
		saveUserToXml(path,generalUsers);
//			readXml();
	}
}
